<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="80%" :title="drawerProps.title">
    <pro-table ref="proTableRef" row-key="id" :columns="columns" :request-api="getJobTriggerPageApi" :init-param="initParam">
      <!-- Expand -->
      <template #expand="{ row }">
        <el-descriptions class="table-expand" :column="4" border>
          <el-descriptions-item label="开始时间" label-align="right" class-name="expand-col">
            {{ row.startTime }}
          </el-descriptions-item>
          <el-descriptions-item label="结束时间" label-align="right" class-name="expand-col">
            {{ row.endTime }}
          </el-descriptions-item>
          <el-descriptions-item label="最近运行时间" label-align="right" class-name="expand-col">
            {{ row.lastRunTime }}
          </el-descriptions-item>
          <el-descriptions-item label="下一次运行时间" label-align="right" class-name="expand-col">
            {{ row.nextRunTime }}
          </el-descriptions-item>
          <el-descriptions-item label="触发次数" label-align="right" class-name="expand-col">
            {{ row.numberOfRuns }}
          </el-descriptions-item>
          <el-descriptions-item label="最大触发次数" label-align="right" class-name="expand-col">
            {{ row.maxNumberOfRuns }}
          </el-descriptions-item>
          <el-descriptions-item label="出错次数" label-align="right" class-name="expand-col">
            {{ row.numberOfErrors }}
          </el-descriptions-item>
          <el-descriptions-item label="最大出错次数" label-align="right" class-name="expand-col">
            {{ row.maxNumberOfErrors }}
          </el-descriptions-item>
          <el-descriptions-item label="重试次数" label-align="right" class-name="expand-col">
            {{ row.numRetries }}
          </el-descriptions-item>
          <el-descriptions-item label="重试间隔时间" label-align="right" class-name="expand-col">
            {{ row.retryTimeout }}
          </el-descriptions-item>
          <el-descriptions-item label="立即启动" label-align="right" class-name="expand-col">
            <el-tag v-if="row.startNow" type="success">是</el-tag>
            <el-tag v-else type="primary">否</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="启动时执行一次" label-align="right" class-name="expand-col">
            <el-tag v-if="row.runOnStart" type="success">是</el-tag>
            <el-tag v-else type="primary">否</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="启动时重置作业" label-align="right" class-name="expand-col">
            <el-tag v-if="row.resetOnlyOnce" type="success">是</el-tag>
            <el-tag v-else type="primary">否</el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </template>
    </pro-table>
  </el-drawer>
</template>

<script setup lang="ts" name="JobTriggerDrawer">
import type { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { ref, computed } from "vue";
import { getJobTriggerPageApi } from "@/api/modules/system/jobTrigger";
import ProTable from "@/components/ProTable/index.vue";

interface DrawerProps {
  title: string;
  isView: boolean;
  param: {
    jobId: string;
  };
}

/** 表格实例 */
const proTableRef = ref<ProTableInstance>();
/** 抽屉是否可见 */
const drawerVisible = ref(false);
/** 抽屉属性 */
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  param: { jobId: "" }
});
/** 表单初始化参数 */
const initParam = computed(() => drawerProps.value.param);
/** 表格配置项 */
const columns: ColumnProps[] = [
  { type: "expand", width: 50 },
  { prop: "triggerId", label: "触发器Id", align: "left" },
  { prop: "triggerType", label: "触发器类型", align: "left" },
  { prop: "args", label: "参数", align: "left" },
  { prop: "description", label: "描述信息", align: "left" },
  {
    prop: "status",
    label: "状态",
    width: 150,
    tag: true,
    enum: [
      { label: "积压", value: 0, tagType: "danger" },
      { label: "就绪", value: 1, tagType: "success" },
      { label: "正在运行", value: 2, tagType: "primary" },
      { label: "暂停", value: 3, tagType: "info" },
      { label: "阻塞", value: 4, tagType: "danger" },
      { label: "由失败进入就绪", value: 5, tagType: "warning" },
      { label: "归档", value: 6, tagType: "info" },
      { label: "崩溃", value: 7, tagType: "danger" },
      { label: "超限", value: 8, tagType: "danger" },
      { label: "无触发时间", value: 9, tagType: "info" },
      { label: "未启动", value: 10, tagType: "info" },
      { label: "未知触发器", value: 11, tagType: "info" },
      { label: "未知程序", value: 12, tagType: "info" }
    ]
  },
  { prop: "nextRunTime", label: "下一次运行时间", width: 180 }
];

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

defineExpose({
  acceptParams
});
</script>

<style lang="scss">
.table-expand {
  padding: 0 8px;
  .expand-col {
    min-width: 180px;
  }
}
</style>
